home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 339_01 / cldemo.c < prev    next >
Text File  |  1990-12-12  |  47KB  |  1,517 lines

  1. /* CLDEMO.C - Demo program for  THE CTRL+C LIBRARY functions */
  2.  
  3. /*
  4. ** CTRLCLIB - The CTRL+C Library(Tm)
  5. ** Copyright 1990 Trendtech Corporation, Inc.
  6. ** All Rights Reserved Worldwide
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12.  
  13. #ifdef __TURBOC__
  14. #define CLEAR_SCREEN     clrscr()
  15. #define _outtext(x)         printf("%s",(x))
  16. #define _settextposition(r,c)         gotoxy(c,r) /* yes, Microsoft has it backwards */
  17. #define _settextwindow(r1,c1,r2,c2)   window(r1,c1,r2,c2)
  18. #define getch            getche
  19. #define DoTimeDelay(x)   delay(x + 50);
  20. #else
  21. #include <graph.h>
  22. #define CLEAR_SCREEN     _clearscreen(_GCLEARSCREEN)
  23. #endif
  24.  
  25. #include <dos.h>
  26. #include <bios.h>
  27. #include <time.h>
  28. #include <string.h>
  29.  
  30. #include "ctrlc.h"   /*  <-- this header must be in every source module */
  31.                      /*      calling a CTRLCLIB function. */
  32.  
  33. /* some neat boolean logic conditions */        
  34. #define FALSE   0
  35. #define TRUE    ~FALSE
  36. #define NO      FALSE
  37. #define YES     ~NO
  38. #define OFF     FALSE
  39. #define ON      ~OFF
  40.  
  41.  
  42. #define HELL_IS_BURNING 1
  43. #define QUIT  (((char)c=='q')||((scn==0x10)&&((char)c=='Q')))
  44.  
  45.  
  46. char *cr[]={"CTRLCLIB - The Ctrl+C Library(Tm)",
  47.             "Copyright 1990 Trendtech Corporation, Inc.",
  48.             "All Rights Reserved Worldwide" };
  49.  
  50.  
  51. /*
  52. ===================================================================
  53. = Function Prototypes
  54. ===================================================================
  55. */
  56.  
  57. int TEST1(void);
  58. int TEST2(void);
  59. int TEST3(void);
  60. int TEST4(void);
  61. int TEST5(void);
  62. int TEST6(void);
  63. int TEST7(void);
  64. int TEST8(void);
  65. int TEST9(void);
  66. int TEST10(void);
  67. int TEST11(void);
  68. int TEST12(void);
  69. int TEST13(void);
  70. int TEST14(void);
  71. int TEST15(void);
  72. int TEST16(void);
  73. int TEST17(void);
  74. int TEST18(void);
  75. int TEST19(void);
  76. int TEST20(void);
  77. int TEST21(void);
  78.  
  79. /*
  80. ===================================================================
  81. = Function Prototypes
  82. ===================================================================
  83. */
  84.  
  85. static void header(char *);
  86. static void footer(void);
  87.  
  88. #ifndef __TURBOC__
  89. static unsigned DoTimeDelay(int);
  90. #endif
  91.  
  92. static unsigned int ReadChar( char * );
  93. static char *spc(void);
  94.  
  95. void IntroScreen(void);
  96. void FinalScreen(void);
  97.  
  98. /*
  99. ===================================================================
  100. = Variables
  101. ===================================================================
  102. */
  103.  
  104. #ifndef __TURBOC__
  105. struct dostime_t t, *pt;
  106. time_t tm;
  107. #endif
  108.  
  109. char s[80], *sp;
  110. unsigned int c, i, scn;
  111.  
  112. /*
  113. ===================================================================
  114. =           M A I N  
  115. ===================================================================
  116. */
  117.  
  118. void main(void)
  119.    {
  120.  
  121. #ifdef __TURBOC__
  122.    delay(0);
  123. #else
  124.    time(&tm);
  125.    srand((unsigned)tm);
  126. #endif
  127.  
  128.    IntroScreen();
  129.  
  130.    while(HELL_IS_BURNING)
  131.       {
  132.       if (TEST1()  == 'Q') break;
  133.       if (TEST2()  == 'Q') break;  
  134.       if (TEST3()  == 'Q') break;  
  135.       if (TEST4()  == 'Q') break;
  136.       if (TEST5()  == 'Q') break;
  137.       if (TEST6()  == 'Q') break;
  138.       if (TEST7()  == 'Q') break;
  139.       if (TEST8()  == 'Q') break;
  140.       if (TEST9()  == 'Q') break;
  141.       if (TEST10() == 'Q') break;
  142.       if (TEST11() == 'Q') break;
  143.       if (TEST12() == 'Q') break;
  144.       if (TEST13() == 'Q') break;
  145.       if (TEST14() == 'Q') break;
  146.       if (TEST15() == 'Q') break;
  147.       if (TEST16() == 'Q') break;
  148.       if (TEST17() == 'Q') break;
  149.       if (TEST18() == 'Q') break;
  150.       if (TEST19() == 'Q') break;
  151.       if (TEST20() == 'Q') break;
  152.       if (TEST21() == 'Q') break;
  153.       break;
  154.       }
  155.  
  156.    CLEAR_SCREEN;
  157.    FinalScreen();
  158.    exit(0);
  159.    }
  160.  
  161.  
  162. /*********************************************************************/
  163.  
  164. int TEST1(void)
  165.    {
  166.    static char far *txt[]={
  167. "This test illustrates COMPLETE trapping of the infamous ^C break.\n",
  168. "Using functions from CTRLCLIB, you can trap the <CTRL>+<C> and\n",
  169. "<CTRL>+<BREAK> keystrokes, as well as eliminate the ^C characters from\n",
  170. "echoing to STDOUT when either of these keys are pressed by the user.\n\n",
  171. "          PRESS  q  to quit this test, or  Q  to quit the demo...\n",
  172. ""};
  173.    
  174.    c = i = 0;
  175.    CLEAR_SCREEN;
  176.    header("TEST 1 - Demonstrate Full CTRL+C and CTRL+BREAK trapping\n\n");
  177.    do { printf("%s",txt[i]); } while (*txt[++i]);
  178.  
  179.    printf("\nPress any key, or try <CTRL>+<C> -or- <CTRL>+<BREAK>...\n\n");
  180.    
  181.    FullCtrlC(DISABLE);          /*  <<--*** CTRLCLIB Function *** */
  182.    
  183.    while(1)
  184.       {
  185.       if ( (i = QueryCtrlC()) != 0 ) /*  <<--*** CTRLCLIB Function *** */
  186.          {
  187.          switch (i)
  188.             {
  189.             case CTRLC: 
  190.                printf(" You pressed the <CTRL>+<C> keys\n");
  191.                break;
  192.             case CTRLBREAK:
  193.                printf(" You pressed the <CTRL>+<BREAK> keys\n");
  194.                break;
  195.             case CTRL2:
  196.                printf(" You pressed the <CTRL>+<2> keys\n");
  197.                break;
  198.             case ALT3:
  199.                printf(" You pressed the <ALT>+<kp-3> keys\n");
  200.                break;
  201.             default:
  202.                printf(" Whoops, this should never happen!!\n");
  203.                break;
  204.             }
  205.          printf("     Press any key...\n");
  206.          continue;
  207.          }
  208.          else
  209.             if ( !kbhit() ) continue;
  210.  
  211.       if ( (c = ReadChar(s)) == 0)       /*  read and print the char typed */
  212.          continue;                       /*  (not a printable char) */
  213.       printf(s);
  214.       if (QUIT) break;
  215.       }
  216.  
  217.    FullCtrlC(ENABLE);           /*  <<--*** CTRLCLIB Function *** */
  218.  
  219.    if (c != 'Q')
  220.       {
  221.       printf("\nCTRL+C and CTRL+BREAK have been ENABLED;  NOW press either set of keys to\n");
  222.       printf("'break' out of this demo program if you wish; -or- press any key to\n");
  223.       printf("continue the next test...");
  224.       getch();
  225.       }
  226.  
  227.    return(c);
  228.    }
  229.  
  230.  
  231. /*********************************************************************/
  232.  
  233. int TEST2(void)
  234.    {
  235.    static char far *txt[]={
  236. "This test illustrates how DOS normally echos a ^C to the screen when\n",
  237. "either <CTRL>+<C> or <CTRL>+<BREAK> is pressed.  Control-break is NOT\n",
  238. "trapped in this test, so it too acts like control-c.  \n\n",
  239. "       PRESS  q  to quit this test, or  Q  to quit demo...\n",
  240. ""};
  241.    
  242.    c = i = 0;
  243.    CLEAR_SCREEN;
  244.    header("TEST 2- Disable CTRL+C and illustrate ^C using DOS print functions");
  245.    do { printf("%s",txt[i]); } while (*txt[++i]);
  246.  
  247.    _settextposition(15,1);
  248.    printf("Now press any key, or try <CTRL>+<C> and <CTRL>+<BREAK>...\n\n");
  249.    
  250.  
  251.    CtrlC(DISABLE);              /*  <<--*** CTRLCLIB Function *** */
  252.  
  253.    while(1)
  254.       {
  255.       i = QueryCtrlC();         /*  <<--*** CTRLCLIB Function *** */
  256.       if (i == CTRLC)  /*  this is TRUE when either CTRL+C -or- CTRL+BREAK is hit */
  257.          {
  258.          printf(" You pressed the <CTRL>+<C> keys\n");
  259.          printf("     Press any key...\n");
  260.          continue;
  261.          }
  262.        else if (i == CTRLBREAK) /*  this will NEVER be true (in this demo section) */
  263.            {
  264.            printf(" You pressed the <CTRL>+<BREAK> keys\n");
  265.            printf("     Press any key...\n");
  266.            continue;
  267.            }
  268.          else
  269.              if ( ! kbhit()) continue;
  270.  
  271.       if ( (c = ReadChar(s)) == 0)           /*  read and print the char typed */
  272.          continue;                         /*  (not a printable char) */
  273.       printf(s);
  274.       if (QUIT) break;
  275.       }
  276.  
  277.    CtrlC(ENABLE);               /*  <<--*** CTRLCLIB Function *** */
  278.  
  279.    if (c != 'Q')
  280.       {
  281.       printf("\n\n\n\nCTRL+C has been ENABLED; press any key ");
  282.       printf("to continue to the next test...\n\n");
  283.       getch();
  284.       }
  285.  
  286.    return(c);
  287.    }
  288.  
  289.  
  290.  
  291. /*********************************************************************/
  292.  
  293. int TEST3(void)
  294.    {
  295.    static char far *txt[]={
  296. "This test shows how the installed interrupt handlers set a flag in the \n",
  297. "CTRLCLIB communication area to indicate which set of keys were pressed.\n",
  298. "In the case of pres